﻿Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports ASPNET = System.Web.UI.WebControls

Partial Class WebForm_ImportVolunteer_ExportPDF
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.btnExport)

        If Not IsPostBack Then
            VolunteerQueryControl1.InitData()
        End If
    End Sub

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Me.Export()
    End Sub

End Class

Partial Class WebForm_ImportVolunteer_ExportPDF
    Private doc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
    Private fontPath As String = MyObj.GetParameter("PDF", "PDFFontPath")
    Private baseFont As BaseFont = baseFont.createFont(fontPath, "Identity-H", baseFont.CACHED)

    Public Sub Export()
        Dim dao As New BLL_ImportVolunteer_SearchRegionVolunteerControl
        Dim dt As Data.DataTable = dao.BLL_Select(VolunteerQueryControl1.OrganizationID, VolunteerQueryControl1.VOrganizationID, VolunteerQueryControl1.VolunteerId, VolunteerQueryControl1.VolunteerName, VolunteerQueryControl1.VolunteerTEL)

        dt.DefaultView.Sort = "RegionName, VolunteerID "
        'Dim doc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)

        Response.ContentType = "application/octet-stream"
        Response.AddHeader("Content-Disposition", "attachment; filename=""VolunteerList.pdf"";")

        PdfWriter.getInstance(doc, Response.OutputStream)

        doc.Open()

        For Each dr As Data.DataRowView In dt.DefaultView
            Me.PrintHeader()
            Me.PrintBody(dr)
            'Me.PrintFooter()
            doc.newPage()
        Next

        doc.Close()
        Response.End()
    End Sub

    Private Sub PrintHeader()
        Dim document As Document = Me.doc
        Dim font As New Font(Me.baseFont, 9.0F, 0)
        Dim font2 As New Font(Me.baseFont, 14.0F, 0)
        Dim font3 As New Font(Me.baseFont, 12.0F, 0)
        document.Add(New Paragraph(" ", font))
        document.Add(New Paragraph(" "))
        document.Add(New Paragraph(" "))
        document.Add(New Paragraph(" "))

        'If Me.VolunteerQueryControl1.Region <> "" Then
        '    Dim dat As New clsData
        '    Dim sql As New MyStringBuilder

        '    sql.AppendFormat("select RegionName from Region ")
        '    sql.AppendFormat("where RegionID = '{0}' ", Me.VolunteerQueryControl1.Region)

        '    document.Add(New Paragraph("                 志工清冊 - " + dat.GetData(sql).Rows(0)(0), font3))
        'Else
        If Me.VolunteerQueryControl1.VOrganizationID <> "0" Then
            Dim dat As New clsData
            Dim sql As New MyStringBuilder

            sql.AppendFormat("select VOrganizationName from VOrganization ")
            sql.AppendFormat("where VOrganizationID = '{0}' ", Me.VolunteerQueryControl1.VOrganizationID)

            document.Add(New Paragraph("                 志工清冊 - " + dat.GetData(sql).Rows(0)(0), font3))
        Else
            document.Add(New Paragraph("                 志工清冊 - ", font3))
        End If

        Dim aTable As New Table(1, 1)
        aTable.WidthPercentage = 88.0F
        aTable.Border = Rectangle.NO_BORDER
        aTable.BorderWidth = 0.0F
        aTable.Padding = 1.0F
        aTable.Spacing = 0.0F

        Dim num As Integer = 9
        Dim aCell As Cell = Nothing

        aCell = New Cell(New Paragraph("                 列印日期 - " + DateTime.Now.ToString("yyyy/MM/dd"), font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.Border = Rectangle.NO_BORDER
        aCell.setHorizontalAlignment(ElementTags.ALIGN_RIGHT)
        aTable.addCell(aCell, New System.Drawing.Point(0, 0))

        'aCell = New Cell(New Paragraph("                   清查人 - ___________", font))
        'aCell.Leading = num
        'aCell.Colspan = 1
        'aCell.Border = Rectangle.NO_BORDER
        'aCell.setHorizontalAlignment(ElementTags.ALIGN_RIGHT)
        'aTable.addCell(aCell, New System.Drawing.Point(1, 0))

        document.Add(aTable)

        'document.Add(New Paragraph("                 姓名：" + dr("VolunteerName").ToString() + " 身份證字號：" + dr("VolunteerID").ToString(), font3))
        'document.Add(New Paragraph(" ", font))
        'document.Add(New Paragraph(" ", font))
        'document.Add(New Paragraph(" ", font))
        'document.Add(New Paragraph(" ", font))
        'document.Add(New Paragraph(" ", font))
        'document.Add(New Paragraph(" "))
        'document.Add(New Paragraph(" "))
        'document.Add(New Paragraph(" "))
        'document.Add(New Paragraph(" "))
        'document.Add(New Paragraph(" "))
        'document.Add(New Paragraph(" "))
    End Sub

    Private Sub PrintBody(ByVal dr As Data.DataRowView)
        Dim num3 As Integer
        Dim document As Document = Me.doc
        Dim font As New Font(Me.baseFont, 9.0F, 0)
        Dim font2 As New Font(Me.baseFont, 14.0F, 0)
        Dim font3 As New Font(Me.baseFont, 12.0F, 0)
        Dim font4 As New Font(Me.baseFont, 7.0F, 0)

        Dim aTable As New Table(4, 1)
        aTable.WidthPercentage = 95.0F
        aTable.Padding = 4.0F
        aTable.Spacing = 4.0F
        aTable.AutoFillEmptyCells = True
        'aTable.setAlignment(ElementTags.ALIGN_CENTER)
        Dim num As Integer = 9
        Dim aCell As Cell = Nothing

        document.Add(New Paragraph("                志工基本資料", font))

        aCell = New Cell(New Paragraph("*姓名", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)

        aTable.addCell(aCell, New System.Drawing.Point(0, 0))
        aCell = New Cell(New Paragraph(dr("VolunteerName"), font))
        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(0, 1))

        aCell = New Cell(New Paragraph("*身份證字號", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 2))
        aCell = New Cell(New Paragraph(dr("VolunteerID"), font))
        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(0, 3))

        aCell = New Cell(New Paragraph("*性別", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(1, 0))
        aCell = New Cell(New Paragraph(dr("GenderText"), font))
        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(1, 1))

        aCell = New Cell(New Paragraph("*生日", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(1, 2))

        If TypeOf dr("Birthday") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("Birthday"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(1, 3))

        aCell = New Cell(New Paragraph("*行動電話", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(2, 0))

        If TypeOf dr("MobilePhone") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("MobilePhone"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(2, 1))

        aCell = New Cell(New Paragraph("*聯絡電話", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(2, 2))

        If TypeOf dr("OfficePhone") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("OfficePhone"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(2, 3))

        aCell = New Cell(New Paragraph("Email", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(3, 0))

        If TypeOf dr("Email") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("Email"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 3
        aTable.addCell(aCell, New System.Drawing.Point(3, 1))

        aCell = New Cell(New Paragraph("*戶籍地", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(4, 0))

        If TypeOf dr("BirthAddress") Is DBNull Then
            aCell = New Cell(New Paragraph(dr("BirthTownName") + " " + dr("BirthRegionName"), font))
        Else
            aCell = New Cell(New Paragraph(dr("BirthTownName") + " " + dr("BirthRegionName") + dr("BirthAddress"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 3
        aTable.addCell(aCell, New System.Drawing.Point(4, 1))


        aCell = New Cell(New Paragraph("*通訊地址", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(5, 0))

        If TypeOf dr("Address") Is DBNull Then
            aCell = New Cell(New Paragraph(dr("TownName") + " " + dr("RegionName"), font))
        Else
            aCell = New Cell(New Paragraph(dr("TownName") + " " + dr("RegionName") + dr("Address"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 3
        aTable.addCell(aCell, New System.Drawing.Point(5, 1))

        aCell = New Cell(New Paragraph("*緊急聯絡人", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(6, 0))

        If TypeOf dr("EmergentPerson") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("EmergentPerson"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(6, 1))

        aCell = New Cell(New Paragraph("*緊急聯絡電話", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(6, 2))

        If TypeOf dr("EmergentPhone") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("EmergentPhone"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(6, 3))

        aCell = New Cell(New Phrase("*教育程度", font))
        aCell.addElement(New Phrase("(註2 可於下方圈選)", font4))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(7, 0))

        If TypeOf dr("EducationLevelText") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("EducationLevelText"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(7, 1))

        aCell = New Cell(New Paragraph("", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(7, 2))

        aCell = New Cell(New Paragraph("", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(7, 3))

        aCell = New Cell(New Phrase("*是否已退休", font))
        aCell.addElement(New Phrase("(註3 可於下方圈選)", font4))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(8, 0))

        If TypeOf dr("RetiredText") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("RetiredText"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(8, 1))

        aCell = New Cell(New Phrase("*職業類別", font))
        aCell.addElement(New Phrase("(註4 可於下方圈選)", font4))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(8, 2))

        If TypeOf dr("OccupationTypeText") Is DBNull Then
            aCell = New Cell(New Paragraph("", font))
        Else
            aCell = New Cell(New Paragraph(dr("OccupationTypeText"), font))
        End If

        aCell.Leading = num
        aCell.Colspan = 1
        aTable.addCell(aCell, New System.Drawing.Point(8, 3))

        'aCell = New Cell(New Paragraph("", font))
        'aCell.Leading = num
        'aTable.addCell(aCell, New System.Drawing.Point(8, 2))

        'aCell = New Cell(New Paragraph("", font))
        'aCell.Leading = num
        'aTable.addCell(aCell, New System.Drawing.Point(8, 3))

        'aCell = New Cell(New Paragraph("職業", font))
        'aCell.Leading = num
        'aCell.Colspan = 1
        'aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        'aTable.addCell(aCell, New System.Drawing.Point(8, 2))

        'If TypeOf dr("OccupationText") Is DBNull Then
        '    aCell = New Cell(New Paragraph("", font))
        'Else
        '    aCell = New Cell(New Paragraph(dr("OccupationText"), font))
        'End If

        'aCell.Leading = num
        'aCell.Colspan = 1
        'aTable.addCell(aCell, New System.Drawing.Point(8, 3))

        Dim element As New Table(1, 1)
        element.WidthPercentage = 88.0F
        element.Border = Rectangle.NO_BORDER
        element.BorderWidth = 0.0F
        element.Padding = 1.0F
        element.Spacing = 4.0F

        element.insertTable(aTable)
        document.Add(element)

        document.Add(New Paragraph("                註1: *號欄位必須要有資料", font))
        document.Add(New Paragraph("                註2: 教育程度代碼 - A 不識字 / B 國小 / C 國中 / D 高中職 / E 大學及專科 / F 研究所及以上 ", font))
        document.Add(New Paragraph("                註3: 是否退休代碼 - A 否，仍在工作 / B 是，已經退休", font))
        document.Add(New Paragraph("                註4: 職業類別代碼 - A 公教人員 / B 軍警人員 / C 農林漁牧 / D 服務業 / E 家庭管理 / F 學生 / G 製造業 / H 其它", font))
        document.Add(New Paragraph(" ", font))

        If TypeOf dr("VBookID") Is DBNull Then
            document.Add(New Paragraph("                紀錄冊編號:                        _____________________ ", font))
        Else
            document.Add(New Paragraph("                紀錄冊編號:                        " + dr("VBookID"), font))
        End If

        document.Add(New Paragraph("                志工隊職務:                        _____________________ ", font))
        document.Add(New Paragraph(" "))

        If dr("TrainingBHour") = 0 Then
            document.Add(New Paragraph("                環保志工服務經歷:                  基礎訓練時數 " + "____" + " 小時 (12) ", font))
        Else
            document.Add(New Paragraph("                環保志工服務經歷:                  基礎訓練時數 " + dr("TrainingBHour").ToString() + " 小時 (12) ", font))
        End If

        If dr("TrainingSHour") = 0 Then
            document.Add(New Paragraph("                                                   特殊訓練時數 " + "____" + " 小時 (10) ", font))
        Else
            document.Add(New Paragraph("                                                   特殊訓練時數 " + dr("TrainingSHour").ToString() + " 小時 (10) ", font))
        End If

        If dr("TrainingWHour") = 0 Then
            document.Add(New Paragraph("                                                   在職訓練時數 " + "____" + " 小時 (40) ", font))
        Else
            document.Add(New Paragraph("                                                   在職訓練時數 " + dr("TrainingWHour").ToString() + " 小時 (40) ", font))
        End If

        If dr("WorkHour") = 0 Then
            document.Add(New Paragraph("                                                   志工服務時數 " + "____" + " 小時 ", font))
        Else
            document.Add(New Paragraph("                                                   志工服務時數 " + dr("WorkHour").ToString() + " 小時 ", font))
        End If

        document.Add(New Paragraph("                可服務時間: ", font))
        aTable = New Table(8, 1)
        aTable.WidthPercentage = 95.0F
        aTable.Padding = 2.0F
        aTable.Spacing = 2.0F
        aTable.AutoFillEmptyCells = True

        aCell = New Cell(New Paragraph(" ", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 0))

        aCell = New Cell(New Paragraph("一", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 1))

        aCell = New Cell(New Paragraph("二", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 2))

        aCell = New Cell(New Paragraph("三", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 3))

        aCell = New Cell(New Paragraph("四", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 4))

        aCell = New Cell(New Paragraph("五", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 5))

        aCell = New Cell(New Paragraph("六", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 6))

        aCell = New Cell(New Paragraph("日", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(0, 7))

        aCell = New Cell(New Paragraph("早上", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(1, 0))

        aCell = New Cell(New Paragraph("中午", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(2, 0))

        aCell = New Cell(New Paragraph("下午", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
        aTable.addCell(aCell, New System.Drawing.Point(3, 0))

        For i As Integer = 1 To 3
            For j As Integer = 1 To 7
                aCell = New Cell(New Paragraph(" ", font))
                aCell.Leading = num
                aCell.Colspan = 1
                aCell.setHorizontalAlignment(ElementTags.ALIGN_CENTER)
                aTable.addCell(aCell, New System.Drawing.Point(i, j))
            Next
        Next

        document.Add(aTable)

        document.Add(New Paragraph(" ", font))
        document.Add(New Paragraph(" ", font))
        document.Add(New Paragraph(" ", font))

        aTable = New Table(2, 1)
        aTable.WidthPercentage = 88.0F
        aTable.Border = Rectangle.NO_BORDER
        aTable.BorderWidth = 0.0F
        aTable.Padding = 1.0F
        aTable.Spacing = 0.0F

        aCell = New Cell(New Paragraph("新北市環保志工網", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.Border = Rectangle.NO_BORDER
        aCell.setHorizontalAlignment(ElementTags.ALIGN_LEFT)
        aTable.addCell(aCell, New System.Drawing.Point(0, 0))

        aCell = New Cell(New Paragraph("http://carpool.ntpc.gov.tw/glife", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.Border = Rectangle.NO_BORDER
        aCell.setHorizontalAlignment(ElementTags.ALIGN_LEFT)
        aTable.addCell(aCell, New System.Drawing.Point(1, 0))

        aCell = New Cell(New Paragraph("                   簽名 : ___________", font))
        aCell.Leading = num
        aCell.Colspan = 1
        aCell.Border = Rectangle.NO_BORDER
        aCell.setHorizontalAlignment(ElementTags.ALIGN_RIGHT)
        aTable.addCell(aCell, New System.Drawing.Point(0, 1))

        document.Add(aTable)

    End Sub

End Class

